//
//  FMlistViewController.m
//  lisure0.1
//
//  Created by xalo on 16/4/20.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "FMlistViewController.h"
#import "FMModel.h"
#import "FMDetailModel.h"
#import "FMdetailTableViewCell.h"
#import "FMDetailHeadView.h"
#import "RadioPlayViewController.h"


#define KFMDetailURL @"http://api2.pianke.me/ting/radio_detail"
#define KFMDetailListURL @"http://api2.pianke.me/ting/radio_detail_list"

@interface FMlistViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic)NSDictionary* radioInfoDic;
@property (strong, nonatomic)NSMutableArray* radioModelArr;
@property (strong, nonatomic)NSMutableDictionary* parameters;
//@property (assign, nonatomic)NSInteger total;

@end

@implementation FMlistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self fetchDataWithURL:KFMDetailURL parameter:self.parameters];
    [self createRefresh];
    [self.tableView registerNib:[UINib nibWithNibName:@"FMdetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"FMdetailTableViewCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)createHeadView{
    UINib* nib = [UINib nibWithNibName:@"FMDetailHeadView" bundle:nil];

    FMDetailHeadView* view = [[nib instantiateWithOwner:nil options:nil] firstObject];
    view.frame = CGRectMake(0, 0, kViewWidth, 300);
    [view.coverimg sd_setImageWithURL:[NSURL URLWithString:self.radioInfoDic[@"coverimg"]] placeholderImage:[UIImage imageNamed:@"IconHome"]];
    view.uname.text = self.radioInfoDic[@"userinfo"][@"uname"];
    [view.icon sd_setImageWithURL:[NSURL URLWithString:self.radioInfoDic[@"userinfo"][@"icon"]] placeholderImage:[UIImage imageNamed:@"arrow"]];
    view.desc.text = self.radioInfoDic[@"desc"];
    view.musicvisitnum.text = [self.radioInfoDic[@"musicvisitnum"] stringValue];
    self.tableView.tableHeaderView = view;
}

-(NSMutableArray *)radioModelArr{

    if (!_radioModelArr) {
        _radioModelArr = [NSMutableArray array];
    }
    return _radioModelArr;
}

-(NSMutableDictionary *)parameters{

    if (!_parameters) {
        _parameters = [NSMutableDictionary dictionaryWithDictionary:@{@"radioid":self.model.radioid}];
    }
    return _parameters;
}

- (void)fetchDataWithURL:(NSString*)url parameter:(NSDictionary*)dic{
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"电台详情数据请求成功");
        NSDictionary* dataDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        self.radioInfoDic = [[dataDic objectForKey:@"data"] objectForKey:@"radioInfo"];
        NSArray* array = [[dataDic objectForKey:@"data"] objectForKey:@"list"];
        for (int i = 0; i < array.count; i ++) {
            FMDetailModel* model = [[FMDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:array[i]];
            [self.radioModelArr addObject:model];
        }
        [self createHeadView];
        [self.tableView reloadData];
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"电台详情数据请求失败");
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.radioModelArr.count;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//
//    UINib* nib = [UINib nibWithNibName:@"FMDetailHeadView" bundle:nil];
//
//    FMDetailHeadView* view = [[nib instantiateWithOwner:nil options:nil] firstObject];
//
//    return view;
//}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FMdetailTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"FMdetailTableViewCell" forIndexPath:indexPath];
    [cell setCellWithModel:self.radioModelArr[indexPath.row]];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RadioPlayViewController* player = [RadioPlayViewController shareRadioPlay];
    player.model = [[self.radioModelArr objectAtIndex:indexPath.row] playinfo];
    player.radiolist = self.radioModelArr;
    player.index = indexPath.row;
    [self.navigationController pushViewController:player animated:YES];
}


-(void)createRefresh{
    static NSInteger start = 0;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        [self.radioModelArr removeAllObjects];
        [self fetchDataWithURL:KFMDetailURL parameter:self.parameters];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        NSDictionary* dic = @{@"radioid":self.model.radioid,@"limit":@10,@"start":@(start)};
        [self fetchDataWithURL:KFMDetailListURL parameter:dic];
        start += 10;
    }];

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
